#!/usr/bin/perl

######################################################################
package Swignition::RDFModel::Property;
######################################################################

use XML::LibXML qw(:all);
use strict;

BEGIN
{
	our $VERSION = 0.1;	
}

sub new
{
	my $class     = shift;
	my $namespace = shift;
	my $localname = shift;
	
	if ( !defined $localname && $namespace =~ /^ (.+[\/\#\?\=\:]) ([^\/\#\?\=\:]+) $/xi )
	{
		$namespace = $1;
		$localname = $2;
	}
	elsif ( !defined $localname )
	{
		$localname = $namespace;
		$namespace = undef;
	}
	
	my $this = {
		namespace  => $namespace,
		localname  => $localname
	};
	bless $this;
}

sub namespace
{
	my $this = shift;
	return $this->{namespace};
}

sub localname
{
	my $this = shift;
	return $this->{localname};
}

sub uri
{
	my $this = shift;
	return $this->namespace . $this->localname;
}

sub vals
{
	my $this = shift;
	my @rv;
	
	foreach my $v (@{$this->{'values'}})
	{
		push @rv, $v->toString;
	}
}

sub valObjects
{
	my $this = shift;
	return $this->{'values'};
}

sub nVals
{
	my $this = shift;
	return scalar @{$this->{'values'}};
}

sub firstVal
{
	my $this = shift;
	return $this->{'values'}->[0];
}

sub addVal
{
	my $this = shift;
	my $val  = shift;
	
	# Prevent duplicates.
	foreach my $v (@{ $this->{'values'} })
	{
		return if ($v->{value} eq $val->{value});
	}
	
#	use Devel::StackTrace;
#	my $trace = Devel::StackTrace->new(max_arg_length=>40);
#	print $trace->as_string; # like carp
	
   push @{ $this->{'values'} }, $val;
	$val->setProperty($this);
}

sub subject
{
	my $this = shift;
	return $this->{subject};
}

sub setSubject
{
	my $this = shift;
	my $subj = shift;
	
	$this->{subject} = $subj;
}

sub doubles
{
	my $this = shift;
	my $prop = $this->uri;
	my @rv;
	
	foreach my $val (@{ $this->{'values'} })
	{
		push @rv, [($prop, $val->toString, $val->type)];
	}
	
	return @rv;
}




1;
